<?php
/**
 * Created by PhpStorm.
 * User: spring
 * Date: 2022/1/11
 * Time: 14:23
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Ckcest extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model(array('ckcest_type' => 'type', 'ckcest_data' => 'data', 'ckcest_yuanshi' => 'yuanshi', 'ckcest_zhuanjia' => 'zhuanjia', 'ckcest_hospital' => 'hospital'));
    }

    public function data($page=1){
        if(!$page || $page == 'false'){ // 查找第一条数据
            $page = 1;

        }
        sleep(1);
        // 医生
//        $key = urlencode('主任医师');
//        $url = 'http://med.ckcest.cn/searchList.do?page='.$page.'&classen=expert&searchText=&searchText2=(%24job_title%3A(%22'.$key.'%22))&sort=score+desc%3Bsort+desc%3Bacademician_letter+asc%3Borgan_i+desc%3Bjob_type+asc%3Bexpert_first_letter+asc&pageNumber=100&userId=';
        // 机构
        $url = 'http://med.ckcest.cn/searchList.do?page='.$page.'&classen=organization&searchText=&searchText2=&sort=have_abstracts+asc%3Bdata_type+asc%3Boverall_rating+desc%3Bgrade_i+desc%3Bfirst_letter+asc%3Bmaster_pilot+desc&pageNumber=100&userId=';
        $file = file_get_contents($url);
        $infos = json_decode($file,true);
        $data = $infos[0]['search']['data'];
        if(empty($data)){
            echo "nodata";
            exit;
        }
        foreach($data as $vo){
            $ins = array(
                'arc_id' => $vo['id'][0],
                'classzh' => '机构', // 如果是医生，就改成和上面$key一样的
                'classen' => $vo['classEn'][0],
                'title' =>$vo['title'][0]
            );
            //if(!$this->data->get_by_where("classen='{$en}' and id='{$vo['id']}'")){
                $this->data->insert($ins);
            //}
        }
        echo $page+1;
    }
    
    public function content($arc_id=''){
        if(!$arc_id || $arc_id == 'false'){ // 查找第一条数据
            $info = $this->data->get_by_order("order by id asc","*","where f2='' and classen='organization'");
            if(!$info){
                echo "nodata";exit;
            }
            $arc_id = $info['arc_id'];
        }
        $url = 'http://med.ckcest.cn/queryDetails.do?id='.$arc_id.'&nameEn=organization';
        sleep(1);
        $ins = array();
        $content = file_get_contents($url);
        $infos = json_decode($content,true);
        $data = $infos[0];
        // 院士
//        $ins['name'] = $data['title'];
//        $ins['buke'] = $data['major'];
//        $ins['university'] = $data['graduate_institution'];
//        $ins['selected_date'] = $data['elected_year'];
//        $ins['intro'] = $data['abstracts'];
        // 专家
//        $ins['name'] = $data['title'];
//        $ins['title'] = $data['job_title'];
//        $ins['unit'] = $data['organization'];
//        $ins['department'] = $data['department'];
//        $ins['jinli'] = $data['abstracts'];
//        $this->zhuanjia->insert($ins);
        // 机构
        $ins['hospital_name'] = $data['title'];
        $ins['base_info'] = $data['grade'];
        $ins['website'] = $data['official_site'];
        $ins['jigou'] = $data['abstracts'];
        $ins['rencai'] = $data['talent_team'];
        $ins['tese'] = $data['featured_departments'];
        $ins['address'] = $data['address'];
        $ins_id = $this->hospital->insert($ins);
        if($ins_id){
            $upd['f2'] = 1;
            $this->data->update($info['id'],$upd);
        }
        echo "ok";
    }

}